package kr.hyunist.androidcomplete02saeildamil.ch26;

import kr.hyunist.androidcomplete02saeildamil.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EngWordEx extends Activity {
	WordDBHelper mHelper;
	EditText mText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.engwordex);
		
		mHelper = new WordDBHelper(this);
		mText = (EditText) findViewById(R.id.engwordet);
	}
	
	public void mOnClick(View v){
		SQLiteDatabase db;
		ContentValues row;
		
		switch(v.getId()){
		case R.id.engwordinsertbtn:
			db = mHelper.getWritableDatabase();
			// 이런 방식 1
			row = new ContentValues();
			row.put("eng", "boy");
			row.put("han", "머스마");
			db.insert("dic", null, row);
			
			// 혹은 이런 방식 2
			db.execSQL("insert into dic values(null, 'girl','가시나');");
			db.execSQL("insert into dic values(null, 'inseo', '농띠!');");
			mHelper.close();
			mText.setText("Insert Success!");
			break;
			 
		case R.id.engworddeletebtn:
			db = mHelper.getWritableDatabase();
			//이런 방식 1
			db.delete("dic", null, null);
			
			//혹은 이런 방식 2
			//db.execSQL("delete from dic");
			mHelper.close();
			mText.setText("Delete Success!");
			break;
			 
		case R.id.engwordupdatebtn:
			db = mHelper.getWritableDatabase();
			//이런 방법 1
			row = new ContentValues();
			row.put("han", "소년");
			db.update("dic", row, "eng = 'boy'", null);
			
			//이런 방법 2
			db.execSQL("update dic set han ='소년' where eng = 'boy';");
			
			mHelper.close();
			mText.setText("Update Success!");
			break;
			 
		case R.id.engwordselectbtn:
			db = mHelper.getReadableDatabase();
			Cursor cursor;
			
			//
			cursor = db.rawQuery("select han, eng from dic", null);
			
			String result = "";
			while(cursor.moveToNext()){
				String han = cursor.getString(0);
				String eng = cursor.getString(1);
				result += (eng + " = " + han + "\n");
			}
			
			if(result.length() == 0){
				mText.setText("Empty!");
			} else {
				mText.setText(result);
			}
			cursor.close();
			mHelper.close();
			
			break;
		}
	}
}
